import { BASE_PATH } from '@/common/api';
import { UserInfo } from '@/common/user';
import { FamilyCreateOrUpdateInfo } from '@/pages/family-counselor/family-newOrUpdate-modal';
import { request } from '@umijs/max';
import {UserCreateOrUpdateInfo} from "@/pages/family-counselor/user-newOrUpdate-modal";

export interface BaseResponse<T> {
  code: string;
  success?: boolean;
  data?: T;
  result: T;
  message: string;
}

export interface PageResp<T> {
  records: T[];
  total: number;
  size: number;
  current: number;
  searchCount?: boolean;
}

export interface UserPageReq {
  current?: number;
  pageSize?: number;
  name?: string;
  role?: string;
  activeFamilyId?: number;
}

export const userLogin = (username: string, password: string): Promise<BaseResponse<UserInfo>> => {
  return request(`${BASE_PATH}/AIFamilyConsultant/admin/user/login`, {
    method: 'POST',
    data: {
      name: username,
      password,
    },
  });
};

export interface FamilyInfo {
  id: number;
  code: string;
  createTime: string;
  focus: boolean;
  lastReportId: number;
  lastReportTime: string;
  name: string;
  reportType: 'day' | 'week' | 'month';
  sessionCount: number;
  yesterdaySessionCount: number;
}

export interface FamilyListReqParam {
  createTime?: string[];
  createEndDate?: string;
  createStartDate?: string;
  focus?: number;
  lastReportTime?: string[];
  lastReportEndDate?: string;
  lastReportStartDate?: string;
  name?: string;
  sessionEnd?: number;
  sessionStart?: number;
  current?: number;
  pageSize?: number;
}

export async function getFamilyList(
  params: FamilyListReqParam,
): Promise<BaseResponse<FamilyInfo[]>> {
  console.log('参数是', params);
  return request(`${BASE_PATH}/AIFamilyConsultant/admin/queryFamilyList`, {
    method: 'POST',
    data: params,
  });
}

/**
 * 删除家庭
 * @param familyId 家庭ID
 */
export async function deleteFamily(familyId: number): Promise<BaseResponse<boolean>> {
  return request(`${BASE_PATH}/AIFamilyConsultant/admin/family/delete/${familyId}`, {
    method: 'DELETE',
  });
}

export interface FamilyReportInfo {
  id: number;
  familyId: number;
  sessionCount: number;
  report: string;
  sessionStartTime?: string;
  sessionEndTime?: string;
}

export interface FamilyReportPageRes {
  records: FamilyReportInfo[];
  total: number;
  size: number;
  current: number;
  pages: number;
}

export interface FamilyRecordDailyCount {
  date: string;
  count: number;
}

export async function queryFamilyReportPage(
  familyId: number | undefined,
  page: number,
  size: number,
): Promise<BaseResponse<FamilyReportPageRes>> {
  console.log('getFamilyList', familyId, page, size);
  return request(
    `${BASE_PATH}/AIFamilyConsultant/admin/queryReportList?familyId=${familyId}&page=${page}&size=${size}`,
  );
}
export async function queryDailyRecordCount(
  familyId: number | undefined,
): Promise<BaseResponse<FamilyRecordDailyCount[]>> {
  console.log('queryDailyRecordCount', familyId);
  return request(
    `${BASE_PATH}/AIFamilyConsultant/admin/queryDailyRecordCount?familyId=${familyId}`,
  );
}

/**
 * 家庭创建
 * @param familyInfo 家庭
 */
export async function familyCreate(
  familyInfo: FamilyCreateOrUpdateInfo,
): Promise<BaseResponse<FamilyInfo>> {
  return request(`${BASE_PATH}/AIFamilyConsultant/admin/create`, {
    method: 'POST',
    data: familyInfo,
  });
}

/**
 * 家庭更新
 * @param familyInfo 家庭
 */
export async function familyUpdate(
  familyInfo: FamilyCreateOrUpdateInfo,
): Promise<BaseResponse<boolean>> {
  return request(`${BASE_PATH}/AIFamilyConsultant/admin/update`, {
    method: 'POST',
    data: familyInfo,
  });
}

/**
 * 家庭创建
 * @param userInfo 家庭
 */
export async function userCreate(
  userInfo: UserCreateOrUpdateInfo,
): Promise<BaseResponse<number>> {
  return request(`${BASE_PATH}/AIFamilyConsultant/admin/user/create`, {
    method: 'POST',
    data: userInfo,
  });
}

/**
 * 家庭更新
 * @param userInfo 家庭
 */
export async function userUpdate(
  userInfo: UserCreateOrUpdateInfo,
): Promise<BaseResponse<boolean>> {
  return request(`${BASE_PATH}/AIFamilyConsultant/admin/user/update`, {
    method: 'POST',
    data: userInfo,
  });
}

/**
 * 家庭用户分页
 * @param userPageReq 分页请求条件
 */
export async function queryUseList(userPageReq: UserPageReq): Promise<BaseResponse<UserInfo[]>> {
  return request(`${BASE_PATH}/AIFamilyConsultant/admin/user/list`, {
    method: 'POST',
    data: userPageReq,
  });
}

/**
 * 删除用户
 * @param userId 用户ID
 */
export async function deleteUser(userId: number): Promise<BaseResponse<boolean>> {
  return request(`${BASE_PATH}/AIFamilyConsultant/admin/user/delete/${userId}`, {
    method: 'DELETE',
  });
}
/**
 * 重置用户密码
 * @param userId 用户ID
 */
export async function resetPassword(userId: number): Promise<BaseResponse<string>> {
  return request(`${BASE_PATH}/AIFamilyConsultant/admin/user/resetPassword`, {
    method: 'POST',
    data: {
      id: userId
    },
  });
}

export interface PromptInfo {
  "reportPrompt"?: string;
  "sessionPrompt"?: string;
}

/**
 * 获取全局统一prompt
 */
export async function adminGetPrompt(): Promise<BaseResponse<PromptInfo>> {
  return request(`${BASE_PATH}/AIFamilyConsultant/admin/adminGetPrompt`);
}

export interface PromptModifyReq {
  reportPrompt: string;
  sessionPrompt: string;
}

/**
 * 设置全局统一prompt
 */
export async function adminSetPrompt(promptInfo: PromptModifyReq): Promise<BaseResponse<boolean>> {
  return request(`${BASE_PATH}/AIFamilyConsultant/admin/adminSetPrompt`, {
    method: 'POST',
    data: promptInfo,
  });
}
